﻿using System;
using System.Linq;
using System.Xml.Linq;
using System.IO;
using System.Reflection;
using System.Configuration;
using System.Collections.Generic;

namespace Shuba.Framework
{
    public class ConfigHelper
    {
        private ConfigHelper() { }

        private static ConfigHelper me = null;

        public static ConfigHelper GetInstance
        {
            get
            {
                me = me ?? new ConfigHelper();
                return me;
            }
        }

        protected string GetFullDirectory()
        {
            string path = Assembly.GetExecutingAssembly().Location;
            return path;
        }

        public Dictionary<string, string> GetAppSettingValues(params string[] keys)
        {
            if (keys.Length < 1) return new Dictionary<string, string>(0);

            Dictionary<string, string> values = new Dictionary<string, string>(keys.Length);
            if (ConfigurationManager.AppSettings[keys[0]] != null)
            {
                for (int i = 0; i < keys.Length; i++)
                    values[keys[i]] = ConfigurationManager.AppSettings[keys[i]];
            }
            else
            {
                string strValue = string.Empty;
                string path = GetFullDirectory();
                string[] listPath = path.Split('\\');

                string masteramcWebConfigDirectory = string.Empty;

                for (int iCounter = 0; iCounter < (listPath.Length - 2); iCounter++)
                {
                    masteramcWebConfigDirectory = masteramcWebConfigDirectory + listPath[iCounter].ToString() + "\\";
                }

                string fileConfig = masteramcWebConfigDirectory + "web.config";
                XDocument xdoc = null;
                try
                { xdoc = XDocument.Load(fileConfig); }
                catch (Exception ex) { }
                if (xdoc != null)
                {
                    for (int i = 0; i < keys.Length; i++)
                        values[keys[i]] = GetSetting(keys[i], xdoc);
                    xdoc = null;
                }
            }
            return values;
        }

        public string GetAppSettingValue(string key)
        {
            string conf = ConfigurationManager.AppSettings[key];
            if (conf != null)
            {
                return conf;
            }
            else
            {
                string strValue = string.Empty;
                string path = GetFullDirectory();
                string[] listPath = path.Split('\\');

                string masteramcWebConfigDirectory = string.Empty;

                for (int iCounter = 0; iCounter < (listPath.Length - 2); iCounter++)
                {
                    masteramcWebConfigDirectory = masteramcWebConfigDirectory + listPath[iCounter].ToString() + "\\";
                }

                string fileConfig = masteramcWebConfigDirectory + "web.config";
                XDocument xdoc = null;
                try
                { xdoc = XDocument.Load(fileConfig); }
                catch (Exception ex) { }
                if (xdoc != null)
                {
                    strValue = GetSetting(key, xdoc);
                    xdoc = null;
                }
                return strValue;
            }
        }

        private string GetSetting(string key, XDocument settingsDocument)
        {
            var query = from n in settingsDocument.Descendants("appSettings")
                        from k in n.Elements()
                        where k.Attribute("key").Value == key
                        select k.Attribute("value").Value;

            return query.FirstOrDefault().Replace("\\\\", "\\");
        }
    }
}
